//
//  LDRefreshView.swift
//  Weibo201655
//
//  Created by pro on 16/5/12.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit

class LDRefreshView: UIView {
    
    
    @IBOutlet weak var arrowView: UIImageView!

    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var tipView: UIView!
    
    @IBOutlet weak var loadingView: UIImageView!
    
    class func refreshView()->LDRefreshView {
        return NSBundle.mainBundle().loadNibNamed("LDRefreshView", owner: nil, options: nil).last as! LDRefreshView
    }
   
    
    func animationWithArrowView(isUP: Bool) {
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.arrowView.transform = isUP ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01)) : CGAffineTransformIdentity
            
        }
        
        titleLabel.text = isUP ? "松开刷新" : "下拉刷新数据"
    }
    
    func loadingAnimation() {
        
        let aniKey = "rotationAni"
        //判断是否已经开始动画
        if let _ = loadingView.layer.animationForKey(aniKey) {
            return
        }
        
        //隐藏箭头
        tipView.hidden = true
        
        //添加动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.toValue = M_PI * 2
        ani.repeatCount = MAXFLOAT
        ani.duration = 0.25
        ani.removedOnCompletion = false
        
        loadingView.layer.addAnimation(ani, forKey: aniKey)
    }
    
    func stopLoadingAni() {
        loadingView.layer.removeAllAnimations()
        tipView.hidden = false
    }

}
